﻿using Autofac;
using PropertyManagment.mapper;
using PropertyManagment.mapper.impl;
using PropertyManagment.service;
using PropertyManagment.service.impl;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace PropertyManagment.config {

    public class DependencyRegisterAttribute : Attribute {

    }

    public class AutofacModule : Autofac.Module {
        protected override void Load(ContainerBuilder builder) {

            //Assembly service = Assembly.Load("PropertyManagment/service/impl");
            //Assembly iservice = Assembly.Load("PropertyManagment/service");
            //builder.RegisterAssemblyTypes(service, iservice)
            //.Where(t => t.FullName.EndsWith("Impl") && !t.IsAbstract) //类名以service结尾，且类型不能是抽象的　
            //    .InstancePerLifetimeScope() //作用域生命周期
            //    .AsImplementedInterfaces()
            //    .PropertiesAutowired(); //属性注入

            //Debug.WriteLine(Assembly.GetExecutingAssembly());
            //Debug.WriteLine(Assembly.Load("PropertyManagment"));

            //通过命名注入
            builder.RegisterAssemblyTypes(Assembly.Load("PropertyManagment"))
                            .Where(t => t.FullName.EndsWith("Impl") && !t.IsAbstract) //类名以service结尾，且类型不能是抽象的　
                            .InstancePerLifetimeScope() //作用域生命周期
                            .AsImplementedInterfaces()  //是以接口方式进行注入
                            .PropertiesAutowired(); //属性注入

            // 通过注解注入
            //builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
            //             .Where(t => t.GetCustomAttribute<DependencyRegisterAttribute>() != null)
            //             .InstancePerLifetimeScope() //作用域生命周期
            //             .AsImplementedInterfaces()
            //             .PropertiesAutowired();
        }
    }
}
